package com.hycreate.platform.common.utils.mypage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PageDemoUtil {

	public static PageDemo Calculation(int total, int current, String type,
			int everyPage) {

		PageDemo pd = new PageDemo();
		int beginIndex;

		int totalPage = PageDemoUtil.Comparison(total, everyPage);// 总页数

		if (type == null) {
			beginIndex = 0;

			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			return pd;
		}

		// 判断是否是首页
		if (type.equals("startPage")) {

			beginIndex = 0;

			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			pd.setCurrentPage(1);

			return pd;
		}
		// 判断是否是末页
		else if (type.equals("endPage")) {
			beginIndex = (totalPage - 1) * everyPage;
			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			pd.setCurrentPage(totalPage);

			return pd;
		}

		else if (isNumeric(type)) {

			int typecurrent = Integer.parseInt(type);
			beginIndex = (typecurrent - 1) * 4;
			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			pd.setCurrentPage(typecurrent);

			return pd;
		} else if (type.equals("hasNextPage")) {
			// 判断传入的页数是否大于总页数

			++current;

			if (totalPage <= current) {

				beginIndex = (totalPage - 1) * everyPage;

				pd.setCurrentPage(totalPage);
			} else {

				beginIndex = (current - 1) * everyPage;

				pd.setCurrentPage(current);
			}

			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			return pd;
		}

		else if (type.equals("hasPrePage")) {

			--current;
			if (current <= 0) {
				beginIndex = 0;
				pd.setCurrentPage(1);
			} else {

				beginIndex = (current - 1) * everyPage;

				pd.setCurrentPage(current);
			}
			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			return pd;
		} else {

			beginIndex = 0;

			pd.setBeginIndex(beginIndex);
			pd.setEveryPage(everyPage);

			pd.setCurrentPage(1);

			return pd;

		}

	}

	public static int Comparison(int total, int everyPage) {

		int totalPage = 0;// 总页数

		if (total % everyPage == 0)
			totalPage = total / everyPage;
		else {
			totalPage = total / everyPage + 1;
		}

		return totalPage;
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

}
